package com.bk.bookviews.controller;

import com.bk.bookviews.domain.Comment;
import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.exception.BusinessExceptionEnum;
import com.bk.bookviews.service.CommentService;
import com.bk.bookviews.util.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bk/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @GetMapping()
    public ResultMsg selectComment(Integer id) {
        Comment comment = commentService.selectComment(id);
        return ResultMsg.successResult(comment);
    }

    @PostMapping("/save")
    public ResultMsg saveComment(@RequestBody Comment comment,@RequestHeader("token") String token){
        commentService.saveComment(comment,token);
        return ResultMsg.successResult();
    }

    @PostMapping("/list")
    @ResponseBody
    public ResultMsg selectStep(@RequestBody Comment comment,@RequestHeader("pageNum") String pageNum, @RequestHeader("pageSize") String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return PageUtils.getPagination(commentService.selectCommentList(comment));
    }

    /**
     * 修改
     */
    @PutMapping()
    public ResultMsg editSave(@RequestBody Comment comment) throws BusinessException {
        if (comment.getId() == null){
            throw new BusinessException(BusinessExceptionEnum.COMMENT_ID_IS_NULL);
        }
        commentService.editSave(comment);
        return ResultMsg.successResult();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public ResultMsg delete(Integer id) throws BusinessException {
        commentService.delete(id);
        return ResultMsg.successResult();
    }
}
